/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/18
 */
layui.define(['form', 'layedit', 'laydate', 'upload'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var layedit = layui.layedit;
    var laydate = layui.laydate;
    let upload = layui.upload;
    let timestamp = new Date().getTime();

    form.render(null, "zmn-brand-form");

    // 自定义验证规则
    form.verify({
        title: function(value){
            if(value.length < 2){ return "分类名称不能少于2个字符"; };
        },
        enname: function(value){
            if(!/^[0-9a-zA-Z ]*$/g.test(value)){
                return "英文名称只能是数字和字母！";
            }
        },
        sort: function(value){
            if (!/^[0-9]+$/.test(value)) {
                return "排序值只能是正整数";
            }
        }
    });

    reLoadImage("icon", json.brand.icon, "iconValue");
    //初始化文件上传插件
    uploaderImage("icon", "iconValue", "png", 1024);

    // 提交
    form.on('submit(zmn-brand-form-submit)', function(obj){
        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/brand/addEdit.action";

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(json) {
                layer.close(openId);
                layer.msg(json.message);
                if(json.status == 200 && json.data > 0) {
                    setTimeout(function () {
                        parent.layui.element.tabChange('layadmin-layout-tabs', "/brand/detail.action");
                        parent.layui.element.tabDelete('layadmin-layout-tabs', window.location.pathname + window.location.search);
                    },1000)

                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){}
        })
        return false;
    });

    /**
     * 描述: 图片上传方法
     * @param elemId
     * @param valueId
     */
    let indexUploadImags;

    function uploaderImage(elemId, valueId, exts, size) {
        upload.render({
            elem: '#' + elemId
            , exts: exts
            , acceptMime: 'image/*'
            , accept: 'images'
            , url: '/product/picture/uploaderImage.action?timestamp=' + timestamp + '&elemId=' + elemId
            , size: size
            , before: function (obj) {
                indexUploadImags = layui.layer.load(1, {
                    shade: [0.5, '#DBDBDB']
                });
            }
            , done: function (res) {
                if (res.code == 2) {
                    var data = res.data;
                    $("#" + valueId).val(data.src);
                    $("#" + elemId).attr("style", "background:url( " + data.src + "?" + Date.now() + ");background-size:100% 100%;background-repeat:no-repeat;");
                    let next = $("#" + valueId).next();
                    let next1 = $(next).next();
                    $(next).attr("style", "visibility:hidden !important");
                    $(next1).attr("style", "visibility:hidden !important");
                } else {
                    layer.msg("上传失败！")
                }
                layui.layer.close(indexUploadImags);
            }
        });
    }

    function reLoadImage(elemId, value, valueId) {
        zmnUploadUtil.reLoadImage($, elemId, value, valueId);
    }

    //对外暴露的接口
    exports('art_brand_detail', {});
});

function deleteImg(element) {
    var elementValue = element+'Value';
    document.getElementById(element).removeAttribute('style');
    Array.from(document.getElementById(element).children).forEach(function(item){
        item.removeAttribute('style');
    });
    document.getElementById(elementValue).removeAttribute('value');
}

function previewImg(element) {
    //调整预览大小
    var img = new Image();
    img.src = document.getElementById(element).value;
    // img.src = obj.src;
    var imgHtml = "<img src='" + img.src + "' width='100%' height='100%'/>";
    //弹出层
    layer.open({
        type: 1,
        shade: 0.8,
        offset: 'auto',
        area: [600 + 'px',300+'px'],
        shadeClose:true,
        scrollbar: false,
        title: "", //不显示标题
        content: imgHtml, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
        cancel: function () {
            //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', { time: 5000, icon: 6 });
        }
    });
}